﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Collections.Specialized;
using Utils;

public partial class account_verify : FBasePage
{
    // Fields

    public string strEmail = "";
    public string strLink = "";

    // Methods
    private void initRecod(Maticsoft.BLL.UserInfo user)
    {
        DataSet list = user.GetList("Recode='" + base.Request["code"].ToString() + "'");
        if (((list != null) && (list.Tables.Count > 0)) && (list.Tables[0].Rows.Count > 0))
        {
            string userName = list.Tables[0].Rows[0]["Username"].ToString();
            string str3 = list.Tables[0].Rows[0]["Password"].ToString();
            string s = list.Tables[0].Rows[0]["id"].ToString();
            string str5 = list.Tables[0].Rows[0]["Manager"].ToString();
            user.UpdateEnable(int.Parse(s), "Y");
            FormsAuthentication.SetAuthCookie(userName, true);
            this.Session["UserName"] = userName;
            string userHostAddress = base.Request.UserHostAddress;
            user.UpdateLoginInfo(int.Parse(s), userHostAddress, DateTime.Now.ToString());
            if (str5.ToUpper() == "Y")
            {
                this.Session["admin"] = s;
            }
            base.SetSuccess("您的帐号激活成功！");
            base.Response.Redirect("/index.aspx");
        }
    }

    private void initSendEmail(Maticsoft.BLL.UserInfo user)
    {
        string sitename = "";
        string str2 = "";
        string mailfrom = "";
        string mailhost = "";
        string item = base.Request["email"].ToString();
        DataSet set = user.GetList("Email='" + item + "'");
        if (((set != null) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
        {
            string str6 = set.Tables[0].Rows[0]["Username"].ToString();
            string str7 = set.Tables[0].Rows[0]["Password"].ToString();
            string str8 = set.Tables[0].Rows[0]["id"].ToString();
            string str9 = set.Tables[0].Rows[0]["Recode"].ToString();
            if (base.ASSystem != null)
            {
                sitename = base.ASSystem.sitename;
                mailfrom = base.ASSystem.mailfrom;
                mailhost = base.ASSystem.mailhost;
            }
            str2 = base.Request.Url.AbsoluteUri.Substring(7);
            str2 = "http://" + str2.Substring(0, str2.IndexOf('/'));
            NameValueCollection values = new NameValueCollection();
            values.Add("username", str6);
            values.Add("sitename", sitename);
            values.Add("wwwprefix", str2);
            values.Add("recode", str9);
            string str10 = WebSiteHelper.LoadTemplate("~/template/mail_verify.html", values);
            string subject = "感谢注册" + base.ASSystem.sitename + "，请验证Email以获取更多服务";
            string body = str10;
            List<string> toMails = new List<string>();
            toMails.Add(item);
            string error = string.Empty;
            if (base.SendMail(toMails, subject, body, out error))
            {
                base.SetSuccess("邮件发送成功！");
                base.Response.Redirect("verify.aspx");
            }
            else
            {
                this.SetError("邮件发送失败！请检查SMTP设置");
                base.Response.Redirect("verify.aspx");
            }
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!this.Page.IsPostBack)
        {
            Maticsoft.BLL.UserInfo user = new Maticsoft.BLL.UserInfo();
            if ((base.Request["code"] != null) && (base.Request["code"].ToString() != ""))
            {
                this.initRecod(user);
            }
            if ((base.Request["email"] != null) && (base.Request["email"].ToString() != ""))
            {
                this.Session["useremail"] = base.Request["email"].ToString();
                this.strEmail = this.Session["useremail"].ToString();
                this.strLink = MailHelper.GetEMailLoginUrl(this.strEmail);
                this.initSendEmail(user);
            }
            if (this.Session["useremail"] != null)
            {
                this.strEmail = this.Session["useremail"].ToString();
            }
        }
    }

}